package interfaceExercises.no2;


import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		ShapeCalculator shapeCalculator;
		Scanner scanner=new Scanner(System.in);
		while (true){
			try {
				System.out.println("请选择形状");
				System.out.println("1.圆形");
				System.out.println("2.矩形");
				System.out.println("0.退出");
				switch (scanner.nextInt()){
					case 1:
						System.out.println("请输入圆的半径");
						shapeCalculator=new Circle(scanner.nextDouble());
						break;
					case 2:
						System.out.println("请输入矩形的长和宽");
						shapeCalculator=new Rectangle(
								scanner.nextDouble(),
								scanner.nextDouble()
						);
						break;
					case 0:
						return;
					default:
						System.out.println("输入数字无效");
						continue;
				}
				System.out.println("面积为"+shapeCalculator.area());
				System.out.println("周长为"+shapeCalculator.circumference());
			}
			catch (InputMismatchException inputMismatchException){
				System.out.println("输入数字无效");
			}
		}
	}
}
interface ShapeCalculator{
	double area();
	double circumference();
}
class Circle implements ShapeCalculator{
	double radius;

	public Circle(double radius) {
		this.radius = radius;
	}

	@Override
	public double area() {
		return Math.PI*radius*radius;
	}

	@Override
	public double circumference() {
		return Math.PI*2*radius;
	}
}
class Rectangle implements ShapeCalculator{
	double length;
	double width;

	public Rectangle(double length, double width) {
		this.length = length;
		this.width = width;
	}

	@Override
	public double area() {
		return length*width;
	}

	@Override
	public double circumference() {
		return 2*(length+width);
	}
}